package stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @author wang'ji'cheng
 * @date 2025/7/19
 */
public class Demo3 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "张三", "张三", "李四", "王五", "张无忌", "张三丰");
        list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
        System.out.println("=========================================");
        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).forEach(s -> System.out.println(s));
        System.out.println("=========================================");

        //limit获取前几个元素
        list.stream().limit(3).forEach(s -> System.out.println(s));

        System.out.println("=========================================");

        //skip跳过几个元素
        list.stream().skip(3).forEach(s -> System.out.println(s));

        System.out.println("==========================================");

        list.stream().skip(2).limit(2).forEach(s -> System.out.println(s));

        System.out.println("============================================");

        list.stream().limit(4).skip(2).forEach(s -> System.out.println(s));

        System.out.println("============================================");

        //去重
        list.stream().distinct().forEach(s-> System.out.println(s));
        System.out.println("=============================================");
        //合并
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "一","二","三");
        Stream.concat(list.stream(),list1.stream()).forEach(s-> System.out.println(s));

        //map映射
        System.out.println("=============================================");
        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2, "张三-23","李四-24","王五-25","王六-25","李其-27");
        list2.stream().map(s->Integer.parseInt(s.split("-")[1])).forEach(s-> System.out.println(s));
    }


}
